class Storage {
  constructor() { }
  //获取某个localstorage值
  get(name) {
    let value = localStorage.getItem(name);
    if (!value) {
      return null;
    }
    try {
      value = JSON.parse(value);
    } catch (e) {
      console.error(e);
    }
    return value;
  }

  //设置localstorage
  set(name, value) {
    localStorage.setItem(name, JSON.stringify(value));
  }
  //清空localstorage
  clear(){
    localStorage.clear();
  }
  //删除某个localstorage
  remove(name) {
    localStorage.removeItem(name);
  }
  //获取session
  getSessionStorage(name){
    let value = sessionStorage.getItem(name);
    if (!value) {
      return null;
    }
    try {
      value = JSON.parse(value);
    } catch (e) {
      console.error(e);
    }
    return value;
  }
  //设置session
  setSessionStorage(name, value) {
    sessionStorage.setItem(name, JSON.stringify(value));
  }
  clearSessionStorage(){
    sessionStorage.clear();
  }
  serialize(obj) {
    let query = '',
      // name,
      value,
      fullSubName,
      // subName,
      subValue,
      innerObj,
      i;

    for (const name in obj) {
      value = obj[name];

      if (value instanceof Array) {
        for (i = 0; i < value.length; ++i) {
          subValue = value[i];
          fullSubName = name + '[' + i + ']';
          innerObj = {};
          innerObj[fullSubName] = subValue;
          query += this.serialize(innerObj) + '&';
        }
      } else if (value instanceof Object) {
        for (const subName in value) {
          subValue = value[subName];
          fullSubName = name + '[' + subName + ']';
          innerObj = {};
          innerObj[fullSubName] = subValue;
          query += this.serialize(innerObj) + '&';
        }
      } else if (value !== undefined && value !== null) {
        query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
      }
    }

    return query.length ? query.substr(0, query.length - 1) : query;
  }
  //设置cookie
  setCookie(name, value,expiredays){
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = name + "=" + escape(value) + ((expiredays == null) ? "" : "^;expires=" + exdate.toUTCString());
    console.log(document.cookie)
  }
  getCookieVal(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
  }
  //获取cookie
  getCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
      var j = i + alen;
      //alert(j);
      if (document.cookie.substring(i, j) == arg) return this.getCookieVal(j);
      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0) break;
    }
    return null;
  }
  //删除cookie
  deleteCookie(name){
    var date=new Date();
    date.setTime(date.getTime()-10000);
    document.cookie=name+"=v; expires="+date.toUTCString();
  }
  //获取缓存中id对应的name
  getAA10NamebyCode(type,code){
    let aa10List = this.getSessionStorage("AA10_"+type);
    if(aa10List && aa10List.length){
      for(let i=0;i<aa10List.length;i++){
        let aa10 = aa10List[i];
        if(aa10['key'] == code){
          return aa10['title'];
        }
      }
    }
    return "";
  }
}

export default new Storage();
